<?php 
use lib\{Examine};
function rethreffun($msg,$send = false){//返回跳转函数
    $currentScript = $_SERVER['REQUEST_URI']; 
    $currentScript = ltrim($currentScript, '/');
    $parts = explode('/', $currentScript);
    $parts[1] = $msg;
    $formattedPath = '/'.implode('', $parts);
    
    return "window.location.href='".$formattedPath."';";
}
function homepage2($url){
    $html = '<html><frameset framespacing="0" border="0" rows="0" frameborder="0">
        <frame name="main" src="'.$url.'" scrolling="auto" noresize>
    </frameset></html>';
    return $html;
}
function xfmp3($price){
    $mp = rand(1,3);
    if($mp==1){
        $mp3 = "//dict.youdao.com/dictvoice?audio=尊敬的客户，你好，你本次订单金额为".$price."元，请按照金额进行付款，否则会导致支付失败,付款后请务必返回本站否则会造成不回调&le=zh";
    }elseif($mp==2){
        $mp3= "//dict.youdao.com/dictvoice?audio=尊敬的用户，你本次交易金额为".$price."元记得不要付错了哟,付款后请务必返回本站否则会造成不回调&le=zh&keyfrom=speaker-target";
    }else{
        $mp3= "//dict.youdao.com/dictvoice?audio=请注意查看付款金额避免支付失败,付款后请务必返回本站否则会造成不回调&le=zh&keyfrom=speaker-target";
    } 
    return $mp3;
}
function pay_type($type = null , $send = false) {
    // 首先检查参数类型
    if (!is_string($type) && $type !== null) {
        return '';  // 如果类型既不是字符串也不是 null，返回空字符串
    }
    
    $types = [
        'qqpay' => 'QQ钱包',
        'alipay' => '支付宝',
        'wxpay' => '微信',
        'usdt' => 'USDT',
        'all' => '统计',
        'bank' => '网银支付',
        'otherpay' => '其他支付'
    ];
    if($send) return $types[$type];
    return ($type !== null && isset($types[$type])) 
        ? '<font color="green">' . $types[$type] . '</font>' 
        : '';
}
function order_status($status){
    
    $data = array(
        '1'=>'<font color=green>已支付</font>',
        '2'=>'<font color=red>已过期</font>',
        '0'=>'<font color=blue>未支付</font>'
        );
	return $data[$status];
}
function notify_status($status,$notify){
    return ($status ==1 && $notify == 0)?'<font color=green>通知成功</font>':(($status == 1 && $notify != 0) ? '<font color=red>通知失败</font>' : '');
}
/**
 * 根据当前时间返回不同的问候语
 * 
 * @param string $name 用户名
 * @return string 返回问候语
 */
function hello_user($name){
    // 获取当前时间的小时、分钟和秒
    $date = date("H:i:s");
    
    // 如果时间在凌晨3点到6点之间
    if($date >= '03:00:00' && $date < '06:00:00'){
        return '夜深了，'.$name.'，愿你有个甜美的梦。';
    // 如果时间在早上6点到8点之间
    }elseif($date >= '06:00:00' && $date < '08:00:00'){
        return '清晨的第一缕阳光照在'.$name.'的脸上，愿你有个充满活力的一天。';
    // 如果时间在上午8点到11点之间
    }elseif($date >= '08:00:00' && $date < '11:00:00'){
        return '美好的一天开始了，'.$name.'，愿你事事顺心。';
    // 如果时间在中午11点到下午1点之间
    }elseif($date >= '11:00:00' && $date < '13:00:00'){
        return '午餐时间到了，'.$name.'，记得好好享受美食哦。';
    // 如果时间在下午1点到5点之间
    }elseif($date >= '13:00:00' && $date < '17:00:00'){
        return '午后的时光总是那么悠闲，'.$name.'，愿你有个愉快的下午。';
    // 如果时间在傍晚5点到7点之间
    }elseif($date >= '17:00:00' && $date < '19:00:00'){
        return '夕阳无限好，'.$name.'，愿你拥有一个轻松的傍晚。';
    // 如果时间在晚上7点到11点之间
    }elseif($date >= '19:00:00' && $date < '23:00:00'){
        return '夜幕降临，'.$name.'，愿你有一个宁静的夜晚。<i class="layui-icon layui-icon-heart-fill" style="color:red"></i>~';
    // 如果时间在深夜11点之后
    }else{
        return '夜深了，'.$name.'，是时候休息了，愿你有个好梦。<i class="layui-icon layui-icon-heart-fill" style="color:red"></i>~';
    }
}

function cookie_zt($zt, $type = NULL, $addtime = NULL, $endtime = NULL, $wx_name = NULL, $i = NULL, $crontime = NULL,$cookie=NULL,$xf_stat=NULL,$xf_type=NULL)
{
    

    if($i == '1'){
        return cookie_zt_pc($zt,$crontime,$type,$xf_type);
    }
    
    if($type == 'wxpay'){
        
        if ($zt != '1') {
            
                if ($i == '0') {
                    return '<font color=red>微信未绑定店员或店员掉线->' . $endtime . '</font>';
                } else if ($i == '2') {
                    if (in_array($xf_type, ['1', '7', '8','9','10'])) {
                        return '<font color=red>微信MAC云端不在线或已掉线</font>';
                    } else if(in_array($xf_type, ['3', '4', '5','6'])){
                        return '<font color=red>微信UOS云端不在线或已掉线</font>';
                    } else if(in_array($xf_type, ['11', '12', '13'])){
                        return '<font color=red>微信IPAD云端不在线或已掉线</font>';
                    }
                }
            } else if ($zt == '1') {
                if ($i == '0') {
                    $login_time = time();
                    $login_wxpay =  Examine::getDB()->select('wechat_trumpet','id',['status'=>1,'login_time'=>['operator'=>'gt','value'=>$login_time] ,'wx_name'=>$wx_name],['limit'=>1,'single_row'=>true])['result'];
                    if (!empty($login_wxpay)) {
                        return '<font color=green>微信已成功绑定,店员已登录</font>';
                    } else {
                        return '<font color=red>微信绑定的店员已掉线,请联系站长处理</font>';
                    }
                } else if ($i == '2') {
                    if (in_array($xf_type, ['1', '7', '8','9','10'])) {
                        return '<font color=green>微信MAC云端状态正常</font>';
                    } else if(in_array($xf_type, ['3', '4', '5','6'])){
                        return '<font color=green>微信UOS云端状态正常</font>';
                    }else if(in_array($xf_type, ['11', '12', '13'])){
                        return '<font color=green>微信IPAD云端状态正常</font>';
                    }
                }
            }
    }else if($type == 'alipay'){
        return $zt == '1' ? '<font color=green>' . ($cookie == 'xfqydmfxy' ? '支付宝当面付' : ($cookie == 'xfqymckxy' ? '支付宝免CK' : '支付宝云端')) . '状态正常</font>' : '<font color=red>支付宝云端不在线或已掉线</font>';
    }else if($type == 'qqpay'){
        return $xf_stat == '1' ? qqpayzt($zt,$xf_type) : '<font color=' . ($zt == '1' ? 'green' : 'red') . '>QQ云端' . ($zt == '1' ? '状态正常' : '不在线或已掉线') . '</font>';
    }else if($type == 'usdt'){
        return '<font color=' . ($zt == '1' ? 'green' : 'red') . '>USDT收款' . ($zt == '1' ? '状态正常' : '异常') . '</font>'; 
    }
}

function cookie_zt_pc($zt, $crontime, $type = null, $xf_type)
{
    // 检查在线状态
    if ($xf_type == 3) {
        $statusMessage = $zt == 1 && $crontime > time() ? 'APP正常在线' : 'APP已掉线';
    } elseif ($xf_type == 1) {
        $statusMessage = $zt == 1 && $crontime > time() ? 'PC正常在线' : 'PC已掉线';
    } else {
        $statusMessage = '未知状态'; // 处理其他情况
    }

    return '<font color=' . ($zt == 1 && $crontime > time() ? 'green' : 'red') . '>' . $statusMessage . '</font>';
}

function qqpayzt($zt,$xf_type){
    $xf_type = ($xf_type == 4)?3:$xf_type;
    $statusText = $zt == '1' ? '状态正常' : '不在线或已掉线';
    return '<font color=' . ($zt == '1' ? 'green' : 'red') . '>QQ' . ($xf_type == '3' ? '手表协议' : ($xf_type == '2' || $xf_type == '1' ? 'PC协议' : ($xf_type == '5' ? 'Linux协议' : ''))) . $statusText . '</font>';
}
function wachat_login_zt($login_time)//微信店员在线状态
{
	if($login_time>=time())
		return '<font color=green>在线</font>';
	else
		return '<font color=red>不在线</font>';
}

function generateParagraphs($data, $defaultFontSize = null) {
    $paragraphs = '';
    foreach ($data as $item) {
        $label = $item['label'];
        $value = $item['value'];
        $fontSize = isset($item['fontSize']) ? $item['fontSize'] : $defaultFontSize;
        $style = "margin-left:40px;";
        if ($fontSize) {
            $style .= " font-size:{$fontSize}px;";
        }

        $paragraphs .= "<p style=\"$style\">$label$value</p>";
    }

    return $paragraphs;
}

//拼接 邮箱模板 
function Mailbox_sending_template($data,$title) {
     global $conf,$httphost;
    $msg = '<div style="border-style:solid; border-width:1px; border-color:#2baee9; width:640px;padding:10px;" >
                <img src="'.$httphost.'assets/img/logo.png"  height="50" width="150">
                <p align="center" style="font-size:26px;">'.$title.'-'.$conf['sitename'].'</p>
                <hr style="border:1 dashed #987cb9" color="#987cb9" size="1">
                <p style="margin-left:50px;">尊敬的'.$data['user'].'您好!</p>'
                . generateParagraphs($data['paragraphs']) .
            '</div>';
    return $msg;
}
function wxyuntypetemp($type){
    $data = [
        1=>"MAC",
        2=>"UOS",
        3=>"IPAD"
    ];
    return $data[$type]?:"未知";
}
function user_noticetemp($title , $text , $time , $send){
    
if($send <=2){
        $html = <<<HTML
        <li class="layui-timeline-item">
            <i class="layui-icon layui-timeline-axis">&#xe63f;</i>
            <div class="layui-timeline-content layui-text">
                <h3 class="layui-timeline-title">{$title}</h3>
                <div class="notice-content">
                    <p>{$text}</p>
                    <div class="notice-footer">
                        <span class="notice-time"><i class="layui-icon layui-icon-time"></i>
                            {$time}
                        </span>
                    </div>
                </div>
            </div>
        </li>
HTML;
}else{
    $html = <<<HTML
        <div class="notice-hidden" style="display: none;">
                <li class="layui-timeline-item">
                <i class="layui-icon layui-timeline-axis">&#xe63f;</i>
                <div class="layui-timeline-content layui-text">
                    <h3 class="layui-timeline-title">{$title}</h3>
                    <div class="notice-content">
                        <p>{$text}</p>
                        <div class="notice-footer">
                            <span class="notice-time"><i class="layui-icon layui-icon-time"></i>
                                {$time}
                            </span>
                        </div>
                    </div>
                </div>
            </li>
        </div>
HTML;
}

    return $html;
}


function user_notice(){
    $sql = "SELECT * FROM `pre_notice` ORDER BY sort ASC";
    $rs = safeSqlQuery($sql,[]);
    
    $html = '';
    $send = 1;
    if($rs['count']>0){
        foreach ($rs['result'] as $res){
            
            if($res['status'] == 1){
                $html .= user_noticetemp($res['title'],$res['datatxt'],$res['addtime'] , $send);
                $send ++;
            }
            
        }
    }
    
    return $html;
}
function checkVipStatus($type) {
    global $userrow,$date;
    switch ($type) {
        case 'alipay':
            if ($userrow['alipay_free_vip_time'] > $date) {
                return '会员：' . $userrow['alipay_free_vip_time'];
            } else {
                return '非会员无法添加二维码';
            }
            break;
        case 'qqpay':
            if ($userrow['qqpay_free_vip_time'] > $date) {
                return '会员：' . $userrow['qqpay_free_vip_time'];
            } else {
                return '非会员无法添加二维码';
            }
            break;
        case 'wxpay':
            if ($userrow['wxpay_free_vip_time'] > $date) {
                return '会员：' . $userrow['wxpay_free_vip_time'];
            } else {
                return '非会员无法添加二维码';
            }
            break;
        default:
            return '未知支付类型';
    }
}
function displaynoneuser(){
    $urlOptions = [
        1 => ['mac' => ''],    // MAC URL 选项
        2 => ['uos' => ''],    // UOS URL 选项
        3 => ['ipad' => '']    // iPad URL 选项
    ];
    
    $wxyunrow = Examine::getDB()->select('wxyunurl', "`id`,`name`,`code`", ['status' => 1]);
    
    if (!empty($wxyunrow['result'])) {
        $urlOptions = array_reduce($wxyunrow['result'], function($carry, $row) {
            $code = $row['code'];
            if (isset($carry[$code])) {
                $key = key($carry[$code]);
                $carry[$code][$key] .= sprintf(
                    '<option value="%d">%s</option>',
                    $row['id'],
                    htmlspecialchars($row['name'], ENT_QUOTES, 'UTF-8')
                );
            }
            return $carry;
        }, $urlOptions);
    }
    
    $macurl = $urlOptions[1]['mac'] ?? '';
    $uosurl = $urlOptions[2]['uos'] ?? '';
    $ipadurl = $urlOptions[3]['ipad'] ?? '';
    $option = '';
    $dyrow = Examine::getDB()->select('wechat_trumpet',"`wx_user`,`wx_name`",['status'=>1]);
    if($dyrow['count']>0){
        $option = '<option value="">请选择店员账号</option>';
        foreach ($dyrow['result'] as $row){
            $option .= '<option value="'.$row['wx_user'].'">'.$row['wx_name'].'</option>';
        }
    }else{
        $option = '<option value="">暂无店员号</option>';
    }
    $html = <<<HTML
    <div id="type_alipay" style="display: none;">
        <div class="layui-form-item">
            <label class="layui-form-label">支付宝挂机协议</label>
            <div class="layui-input-block">
                <select name="alipay" lay-filter="alipay" default="">
                     <option value="">请选择挂机方式</option>
                     <option value="1">云端协议一（平均10-15天 异地未知）</option>
                     <option value="2">本地协议二（平均15-30  异地未知）</option>
                     <option value="3">本地协议三（平均 10-30 异地未知）</option>
                     <option value="4">免CK协议(不掉线)</option>
                     <!--<option value="5">当面付通道(不掉线)</option>-->
                </select>
            </div>
        </div>
    </div>
    <div id="type_alipaymck" style="display: none;">
        <div class="layui-form-item">
			<label class="layui-form-label">
			    支付宝UID
			</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="alipaymck_uid"  value="" />
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">
			    支付宝APPID
			</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="alipaymck_appid"  value="" />
			</div>
		</div>
		<div class="layui-form-item">
            <label class="layui-form-label">支付宝公钥</label>
            <div class="layui-input-block">
                <textarea name="alipaypublicmck_key" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">应用私钥</label>
            <div class="layui-input-block">
                <textarea name="appprivatemck_key" class="layui-textarea"></textarea>
            </div>
        </div>
    </div>
    <div id="type_alipaydmf" style="display: none;">
        <div class="layui-form-item">
			<label class="layui-form-label">
			    支付宝APPID
			</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="alipaydmf_appid"  value="" />
			</div>
		</div>
		<div class="layui-form-item">
            <label class="layui-form-label">支付宝公钥</label>
            <div class="layui-input-block">
                <textarea name="alipaypublicdmf_key" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">应用私钥</label>
            <div class="layui-input-block">
                <textarea name="appprivatedmf_key" class="layui-textarea"></textarea>
            </div>
        </div>
    </div>
    <div id="type_qqpay" style="display: none;">
        <div class="layui-form-item">
            <label class="layui-form-label">QQ挂机协议</label>
            <div class="layui-input-block">
                <select name="qqpay" lay-filter="qqpay" default="">
                     <option value="">请选择协议</option>
                     <option value="1">云端协议（两天左右掉线）</option>
                     <option value="2">QQPC协议</option>
                     <!--<option value="3">QQ手表协议一</option>
                     <option value="4">QQ手表协议二</option>-->
                </select>
            </div>
        </div>
    </div>
    <div id="type_wxpay" style="display: none;">
        <div class="layui-form-item">
            <label class="layui-form-label">微信挂机协议</label>
            <div class="layui-input-block">
                <select name="wxpay" lay-filter="wxpay" default="">
                     <option value="">请选择协议</option>
                     <option value="1">微信店员</option>
                     <option value="2">Mac云端</option>
                     <!--<option value="3">UOS云端</option>-->
                     <option value="4">IPAD云端</option>
                </select>
            </div>
        </div>
    </div>
    <div id="type_wxpaymac" style="display: none;">
        <div class="layui-form-item">
            <label class="layui-form-label">请选择挂机类型(Mac)</label>
            <div class="layui-input-block">
                <select name="mactype" lay-filter="mactype" default="">
                     <option value="">请选择要添加的通道</option>
                     <option value="1">个码免输通道</option>
                     <!--<option value="7">个人收款单免输通道</option>-->
                     <option value="8">微信赞赏码通道(不免输)</option>
                     <option value="9">经营码通道(不免输)</option>
                     <option value="10">小微商家通道(不免输)</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">请选择云端地址(MAC)</label>
            <div class="layui-input-block">
                <select name="macwxyunurl" lay-filter="macwxyunurl" default="">
                     <option value="">请选择云端地址(选择这个则使用默认云端)</option>
                     $macurl
                </select>
            </div>
        </div>
    </div>
    <div id="type_wxpayUos" style="display: none;">
        <div class="layui-form-item">
            <label class="layui-form-label">请选择挂机类型(UOS)</label>
            <div class="layui-input-block">
                <select name="Uostype" lay-filter="Uostype" default="">
                     <option value="">请选择要添加的通道</option>
                   <option value="3">个码通道(不免输)</option>
                   <option value="4">微信赞赏码通道(不免输)</option>
                   <option value="5">经营码通道(不免输)</option>
                   <option value="6">小微商家通道(不免输)</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">请选择云端地址(UOS)</label>
            <div class="layui-input-block">
                <select name="uoswxyunurl" lay-filter="uoswxyunurl" default="">
                     <option value="">请选择云端地址(选择这个则使用默认云端)</option>
                     $uosurl
                </select>
            </div>
        </div>
     </div>
    <div id="type_wxpayIpad" style="display: none;">
        <div class="layui-form-item">
            <label class="layui-form-label">请选择挂机类型(Ipad)</label>
            <div class="layui-input-block">
                <select name="Ipadtype" lay-filter="Ipadtype" default="">
                     <option value="">请选择要添加的通道</option>
                   <option value="11">个码通道(免输)</option>
                   <option value="12">经营码链接(免输)</option>
                   <option value="13">经营码通道(免输)</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">请选择云端地址(Ipad)</label>
            <div class="layui-input-block">
                <select name="ipadwxyunurl" lay-filter="ipadwxyunurl" default="">
                     <option value="">请选择云端地址(选择这个则使用默认云端)</option>
                     $ipadurl
                </select>
            </div>
        </div>
    </div>
    <div id="type_wxpaysc" style="display: none;">
        <div class="layui-form-item">
            <label class="layui-form-label">选择二维码</label>
            <div class="layui-input-block">
                <button type="button" class="pear-btn pear-btn-primary pear-btn-sm  upload-image" id="ongImg"><i class="fa fa-image"></i>上传图片</button>
                
            </div>
        </div>
    </div>
    <div id="type_wxpayurl" style="display: none;">
        <div class="layui-form-item">
			<label class="layui-form-label">
			    二维码地址
			</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="qr_url" id="qr_url"  value="" placeholder="上传二维码自动识别,可修改"/>
			</div>
		</div>
    </div>
    <div id="type_wxpaydy" style="display: none;">
        <div class="layui-form-item">
            <label class="layui-form-label">请选择微信店员</label>
            <div class="layui-input-block">
                <select name="wx_name" lay-filter="wx_name" default="">
                     $option
                </select>
            </div>
        </div>
    </div>
    
HTML;
    return $html;
}

function generateAlipayInfo($row){
    $html = '';
    if ($row['cookie'] == 'xfqymckxy') {
        $html .= <<<HTML
        <div class="layui-form-item">
            <label class="layui-form-label">
                支付宝UID
            </label>
            <div class="layui-input-block">
                <input type="text" class="layui-input"  value="{$row['alipayUId']}" readonly autocomplete="off"/>
            </div>
        </div>
HTML;
    }
    $html .=<<<HTML
    <div class="layui-form-item">
            <label class="layui-form-label">
                支付宝APPID
            </label>
            <div class="layui-input-block">
                <input type="text" class="layui-input"  value="{$row['alipayAppId']}" readonly autocomplete="off"/>
            </div>
        </div>
    <div class="layui-form-item">
        <label class="layui-form-label">支付宝公钥</label>
        <div class="layui-input-block">
            <textarea  class="layui-textarea" readonly autocomplete="off">{$row['alipayPublicKey']}</textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">支付宝私钥</label>
        <div class="layui-input-block">
            <textarea  class="layui-textarea" readonly autocomplete="off">{$row['appPrivateKey']}</textarea>
        </div>
    </div>
HTML;

    return $html;
}
function submit_head(){
    echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html>
        <head>
        	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        	<title>正在为您跳转到支付页面，请稍候...</title>
            ';
}

function user_money_meal_temp($meals = []){
    $html = '<div class="layui-bg-gray" style="padding: 15px; margin-bottom: 15px; border-radius: 5px;">
        <h3 style="font-size: 16px; font-weight: bold; margin-bottom: 10px;">额度套餐：</h3>
        <div class="layui-row layui-col-space15">'; 

    foreach ($meals as $meal){
        $html .= '
            <div class="layui-col-md6">
                <div class="layui-card">
                    <div class="layui-card-body" style="padding: 15px; text-align: center;">
                        <p style="font-size: 14px; color: #333;">
                            充值 <span style="color: #009688; font-weight: bold;">'.$meal['recharge'].'</span> 元
                            得 <span style="color: #009688; font-weight: bold;">'.$meal['money'].'</span> 元额度
                        </p>
                    </div>
                </div>
            </div>
        ';
    }

    $html .='</div></div>';
    return $html;
}

function Web_Type(){
    $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
    switch($agent){
        case (strpos($agent, 'alipayclient') !== false):
             return 'alipay';
            break;
        case (strpos($agent, 'micromessenger') !== false):
            return 'wxpay';
            break;
        case (preg_match('/qq\/[\d]/i', $agent) && strpos($agent, 'mqqbrowser') !== false):
            return 'qqpay';
            break;
        default :
            return false;
            break;
    }
}
?>